package com.tky.conference.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.tky.conference.R;
import com.tky.conference.base.BaseActivity;
import com.tky.conference.base.BaseApplication;
import com.tky.conference.dao.AppointInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.entity.LoginOutInfo;
import com.tky.conference.implInterfaces.AppointInfoImpl;
import com.tky.conference.implInterfaces.LogoutImpl;
import com.tky.conference.interfaces.HttpResultListener;
import com.tky.conference.utlils.ShowDialog;
import com.tky.conference.utlils.ToastUtil;
import com.tky.conference.utlils.UIUtils;

import java.io.Serializable;
import java.util.List;


public class MainActivity extends BaseActivity implements View.OnClickListener,HttpResultListener {

    private Button myConference;
    private Button appiontConference;
    private Toolbar toolbar;
    private LogoutImpl logoutImpl;
    private LoginOutInfo loginOutInfobj;
    private ShowDialog showDialog;
    private String account;
    private List<AppointInfo> appointInfoList;
    @Override
    public void initView() {
        setContentView(R.layout.activity_main);
        myConference= (Button) findViewById(R.id.myConference);
        appiontConference= (Button) findViewById(R.id.conference);
        showDialog=new ShowDialog(this);
    }

    @Override
    public void initDates() {
        SharedPreferences sharedPreferences=MainActivity.this.getSharedPreferences("save_account",MODE_PRIVATE);
        account=sharedPreferences.getString("account",null);
        showDialog=new ShowDialog(MainActivity.this);
    }

    @Override
    public void initToolBar() {
         toolbar= (Toolbar) findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
        toolbar.setTitle("会议类型");

    }

    @Override
    public void initEvent() {
        myConference.setOnClickListener(this);
        appiontConference.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //打开我的会议
            case R.id.myConference:
                Intent myintent=new Intent(BaseApplication.getContext(),ConferenceRecordActivity.class);
                UIUtils.startActivity(myintent);
                break;
            //预约会议
            case R.id.conference:
                //开始请求数据
                AppointInfoImpl appointInfoImpl=new AppointInfoImpl(this);
                appointInfoImpl.getAppointInfo(account, "1");
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.i("exitexit=======","okokok");
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            IsExit();
            return true;
        }
//        if(id==android.R.id.home){
//            this.finish();
//            return true;
//        }
        return super.onOptionsItemSelected(item);
    }


    //是否确认退出
    public void IsExit(){
        new AlertDialog.Builder(this).setTitle("确认退出吗？").setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        logoutImpl=new LogoutImpl(new HttpResultListener() {
                            @Override
                            public void onStartRequest() {
                                showDialog.Loading("退出登录...");
                            }

                            @Override
                            public void onResult(Object obj) {
                                showDialog.dismissDialog();
                                if (obj instanceof FailRequest) {
                                    Log.i("提交事件响应失败。。。", "okokok");
                                    if (!((FailRequest) obj).getStatus().equals(0)) {
                                        Toast.makeText(BaseApplication.getContext(), ((FailRequest) obj).getMsg(), Toast.LENGTH_SHORT).show();
                                    }
                                }else {
                                    loginOutInfobj = (LoginOutInfo) obj;
                                    if(loginOutInfobj.getStatus().equals("0")){
                                        Log.i("提交事件响应。。。","okokok");
                                        MainActivity.this.finish();
                                    }
                                }
                            }

                            @Override
                            public void onFinish() {

                            }
                        });
                        logoutImpl.loginOut("wuxiaomin");
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //取消，不做任何事情
            }
        }).show();
    }

    @Override
    public void onStartRequest() {
        showDialog.Loading("加载数据...");
    }

    @Override
    public void onResult(Object obj) {
        if (obj instanceof FailRequest) {
            showDialog.dismissDialog();
            if (!((FailRequest) obj).getStatus().equals(0)) {
                ToastUtil.showToast(this, ((FailRequest) obj).getMsg());
            }
        } else {
            showDialog.dismissDialog();
            appointInfoList = (List<AppointInfo>) obj;
            Intent intent = new Intent(BaseApplication.getContext(), ConferenceActivity.class);
            intent.putExtra("APPINFOLIST", (Serializable) appointInfoList);
            UIUtils.startActivity(intent);
        }
    }
        @Override
        public void onFinish () {

        }
}
